package cn.stylefeng.guns.modular.manager.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.FlowService;
import cn.stylefeng.guns.modular.manager.model.params.FlowServiceParam;
import cn.stylefeng.guns.modular.manager.service.FlowServiceService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;


/**
 * 服务控制器
 *
 * @author yumker
 * @Date 2021-01-07 17:22:24
 */
@Controller
@RequestMapping("/flowService")
public class FlowServiceController extends BaseController {

    private String PREFIX = "/manager/flowService";

    @Autowired
    private FlowServiceService flowServiceService;

    /**
     * 跳转到主页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/flowService.html";
    }

    /**
     * 新增页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/flowService_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/flowService_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FlowServiceParam flowServiceParam) {
        if(!StringUtils.isEmpty(flowServiceParam.getNotes())){
            try {
                flowServiceParam.setNotes(URLDecoder.decode(flowServiceParam.getNotes(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if(!StringUtils.isEmpty(flowServiceParam.getPayNotes())){
            try {
                flowServiceParam.setPayNotes(URLDecoder.decode(flowServiceParam.getPayNotes(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        if(!StringUtils.isEmpty(flowServiceParam.getPoints())){
            try {
                flowServiceParam.setPoints(URLDecoder.decode(flowServiceParam.getPoints(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.flowServiceService.add(flowServiceParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FlowServiceParam flowServiceParam) {
        if(!StringUtils.isEmpty(flowServiceParam.getNotes())){
            try {
                flowServiceParam.setNotes(URLDecoder.decode(flowServiceParam.getNotes(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if(!StringUtils.isEmpty(flowServiceParam.getPayNotes())){
            try {
                flowServiceParam.setPayNotes(URLDecoder.decode(flowServiceParam.getPayNotes(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        if(!StringUtils.isEmpty(flowServiceParam.getPoints())){
            try {
                flowServiceParam.setPoints(URLDecoder.decode(flowServiceParam.getPoints(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.flowServiceService.update(flowServiceParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FlowServiceParam flowServiceParam) {
        this.flowServiceService.delete(flowServiceParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FlowServiceParam flowServiceParam) {
        FlowService detail = this.flowServiceService.getById(flowServiceParam.getServiceId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FlowServiceParam flowServiceParam) {
        return this.flowServiceService.findPageBySpec(flowServiceParam);
    }

}


